$("#startDay").datepicker({
    format: 'yyyy-mm-dd'
});

$("#endDay").datepicker({
    format: 'yyyy-mm-dd'
});

// 请假时长计算
function subLeave() {
    // 获取选中请假类型
    var sumTime = $("#sumTime option:selected").val();
    //console.log("选中值：" + sumTime);

    // 获取类型返回值
    var leaveType = sumTime.substring(0, 2);
    //console.log("选中值截取：" + leaveType);

    if (leaveType == "01") {
        leaveType = "年假";
    }
    if (leaveType == "02") {
        leaveType = "病假";
    }
    if (leaveType == "03") {
        leaveType = "事假";
    }
    if (leaveType == "04") {
        leaveType = "产假";
    }
    if (leaveType == "05") {
        leaveType = "陪产假";
    }
    if (leaveType == "06") {
        leaveType = "哺乳假";
    }
    if (leaveType == "07") {
        leaveType = "节育假";
    }
    if (leaveType == "08") {
        leaveType = "丧假";
    }
    if (leaveType == "09") {
        leaveType = "工伤假";
    }
    if (leaveType == "10") {
        leaveType = "档假";
    }
    if (leaveType == "11") {
        leaveType = "婚假";
    }
    if (leaveType == "12") {
        leaveType = "产检假";
    }
    //console.log("请假类型：" + leaveType);

    // 获取剩余假期时长
    var leaveTime = sumTime.substring(6, 8);
    //console.log("剩余假期时长：" + leaveTime);

    // 获取开始日期时间
    var startDay = $("#startDay").val();
    var startTime = $("#startTime option:selected").val();
    var startDT = startDay + " " + startTime;
    //console.log("开始日期时间：" + startDT);

    // 获取结束日期时间
    var endDay = $("#endDay").val();
    var endTime = $("#endTime option:selected").val();
    var endDT = endDay + " " + endTime;
    //console.log("结束日期时间：" + endDT);

    if (startDay == "" || startDay == null || startDay == undefined) {
        layer.msg("开始时间不能为空！");
        return;
    }
    if (endDay == "" || endDay == null || endDay == undefined) {
        layer.msg("结束时间不能为空！");
        return;
    }

    // 转换开始标准时间
    var start = new Date(startDT);
    //console.log("开始标准时间：" + start);
    // 转换结束标准时间
    var end = new Date(endDT);
    //console.log("结束标准时间：" + end);
    // 当前日期及时间
    var toDay = new Date();
    //console.log("当前标准时间：" + toDay);

    if (Date.parse(start) < Date.parse(toDay)) {
        layer.msg("开始时间不能小于当前时间！");
        return;
    }
    if (Date.parse(end) < Date.parse(toDay)) {
        layer.msg("结束时间不能小于当前时间！");
        return;
    }
    if (Date.parse(end) == Date.parse(start)) {
        layer.msg("结束时间不能等于开始时间！");
        return;
    }
    if (Date.parse(end) < Date.parse(start)) {
        layer.msg("结束时间不能小于开始时间！");
        return;
    }

    // 获取时间
    var starttime = startDT;
    var endtime = endDT;

    // 转换时间标准
    var datetime1 = new Date(starttime),
        datetime2 = new Date(endtime);

    // 重置时间
    var date1 = datetime1.setHours(0, 0, 0); //开始日期，毫秒表示
    var date2 = datetime2.setHours(0, 0, 0); //结束日期，毫秒表示

    //获取时间到月份
    var starttimeleft = starttime.substring(0, 10);
    var endtimeleft = endtime.substring(0, 10);

    // 转换时间标准
    var beginDate = new Date(starttimeleft.replace(/-/g, "/"));
    var endDate = new Date(endtimeleft.replace(/-/g, "/"));

    // 开始结束天数间隔
    var dayNum = parseInt(Math.abs(beginDate - endDate) / 1000 / 60 / 60 / 24);

    // 开始结束周数间隔
    var startWeek = beginDate.getDay();

    // 截取时间保留小时
    var starHour = Number(starttime.substring(10, 13));
    var endHour = Number(endtime.substring(10, 13));

    // 截取时间保留分钟
    var starMinutes = Number(starttime.substring(14, 16));
    var endMinutes = Number(endtime.substring(14, 16));

    if (starMinutes != endMinutes) {
        layer.msg("请假最小时长计量单位：一小时");
        return;
    }
    // 计算请假时长
    else {
        // 保存请假小时数
        var travelHours = 0;
        var travelTimeHours = 0;
        var iNow = 0;

        var dayinweek = new Date(starttime).getDay(); // 开始日期是星期几
        var dayendweek = new Date(endtime).getDay(); // 结束日期是星期几   

        if (dayinweek == 6 || dayinweek == 0) {
            layer.msg("开始日期不能为周六或周末");
            return;
        }
        if (dayendweek == 6 || dayendweek == 0) {
            layer.msg("结束日期不能为周六或周末");
            return;
        }

        // 根据上班时间 9:00--18:00  八小时
        // 变换到工作时区内
        if (date1 == date2) { //开始结束时间均在一天
            if (starHour < 12 && endHour > 12) {
                travelHours = endHour - starHour - 1;
            } else {
                travelHours = endHour - starHour;
            }
            //console.log("单个工作日请假时长：" + travelHours + "小时");

            // 判断是否能够提交请假
            if (leaveTime < travelHours) {
                layer.msg("剩余请假时长不足，无法提交请假申请，请联系行政管理人员！");
                return;
            }

            // 请假时长赋值
            $("#long").val(travelHours);
        } else {
            //如果开始时间小于8点，则计算工时的时候从8点开始计算
            if (starHour < 9) {
                starHour = 9;
            }
            //如果开始时间大于17点，
            if (starHour > 18) {
                starHour = 18;
            }
            if (endHour < 9) {
                endHour = 9;
            }
            //1.3
            if (endHour > 18) {
                endHour = 18;
            }
            if (0 < dayinweek && dayinweek < 6 && dayendweek == 0 || 0 < dayinweek && dayinweek < 6 && dayendweek == 6) {
                travelTimeHours += (weekendBetween(dayNum) - 1) * 8;
            } else if (0 < dayendweek && dayendweek < 6 && dayinweek == 0 || 0 < dayendweek && dayendweek < 6 && dayinweek == 6) {
                travelTimeHours += (weekendBetween(dayNum) - 1) * 8;
            } else if (dayinweek != 6 && dayinweek != 0 && dayendweek != 6 && dayendweek != 0) {
                travelTimeHours += (weekendBetween(dayNum) - 2) * 8;
            }
            if (dayinweek != 6 && dayinweek != 0) {
                if (starHour >= 9 && starHour <= 12) {
                    travelTimeHours += 17 - starHour;
                }
                if (starHour >= 13 && starHour <= 18) {
                    travelTimeHours += 18 - starHour;
                }
            }
            if (dayendweek != 6 && dayendweek != 0) {
                if (endHour >= 9 && endHour <= 12) {
                    travelTimeHours += endHour - 9;
                }
                if (endHour == 13) {
                    travelTimeHours += 3;
                }
                if (endHour == 14) {
                    travelTimeHours += 4;
                }
                if (endHour == 15) {
                    travelTimeHours += 5;
                }
                if (endHour == 16) {
                    travelTimeHours += 6;
                }
                if (endHour == 17) {
                    travelTimeHours += 7;
                }
                if (endHour == 18) {
                    travelTimeHours += 8;
                }
            }

            var timeDays = travelHours;
            var travelTime = travelTimeHours;
            var travelTimeDay = timeDays + travelTime;

            //console.log("多个工作日请假时长：" + travelTimeDay + "小时");

            // 判断是否能够提交请假
            if (leaveTime < travelTimeDay) {
                layer.msg("剩余请假时长不足，无法提交请假申请，请联系行政管理人员！");
                return;
            }

            // 请假时长赋值
            $("#long").val(travelTimeDay);

            // 去除周六日
            function weekendBetween(dayNum) {
                var iNow = 0;
                dayNum = dayNum + 1;
                for (var i = 0; i < dayNum; i++) {
                    var firstDay = startWeek % 7;
                    if (firstDay == 0) firstDay = 7;
                    if (firstDay < 6) {
                        iNow++;
                    }
                    if (firstDay == 7) {
                        firstDay = 1;
                    }
                    startWeek++;
                }
                return iNow;
            }
        }
    }
};